<template>
  <div class="bg">
    <el-card class="box-card">
      <el-row type="flex" class="row-bg" justify="end">
        <el-col :span="6" :offset="24">
          <el-input style="width:300px" v-model="selectCode" placeholder="请输入商家名称/联系人/手机号码查询"></el-input>
        </el-col>
        <el-col :span="6" :offset="1">
          <el-date-picker
            v-model="startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始日期"
          ></el-date-picker>
        </el-col>
        <el-col :span="6" :offset="1">
          <el-date-picker
            v-model="endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束日期"
          ></el-date-picker>
        </el-col>

        <el-col :span="6" :offset="1">
          <div style="display:flex">
            <el-button type="primary" @click="search">查询</el-button>
            <el-button type="success" @click="reset ">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="end">
          <el-col :span="2">
            <el-button type="success" @click="addcommodity">导出数据</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="index" width="100" label="序号" align="center"></el-table-column>
        <el-table-column prop="businessContact" label="联系人" align="center"></el-table-column>
        <el-table-column prop="businessContactPhone" label="联系电话" align="center"></el-table-column>
        <el-table-column prop="businessName" label="商家名称" align="center"></el-table-column>
        <el-table-column prop="businessAddress" label="详细地址" align="center"></el-table-column>
        <el-table-column prop="businessHours" label="营业时间" align="center"></el-table-column>
        <el-table-column prop="businessIntroduced" label="服务介绍" align="center"></el-table-column>
        <el-table-column label="店铺照" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.businessBanner[0]"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="店铺封面" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.businessCoverImg"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="营业执照" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.businessLicense"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="zip" label="状态" width="100" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.businessStatus==1?'有效':'待审核'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="businessCreateTime" label="注册时间" width="170"></el-table-column>
        <el-table-column fixed="right" label="操作" width="220">
          <template slot-scope="scope">
            <el-button @click="lookcheck(scope.row)" type="primary" size="small">查看</el-button>
            <el-button type="warning" size="small" @click="dialog(scope.row)">审核</el-button>
            <el-button type="danger" size="small" @click="deletmerchant(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :current-page="current"
        style="margin-top:24px"  
        @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>

    <!-- 弹窗审核 -->
    <el-dialog title="审核" :visible.sync="dialogVisible" width="25%"  @close="closeDialog">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="审核" prop="resource">
          <el-radio-group v-model="ruleForm.resource">
            <el-radio :label="1">审核通过</el-radio>
            <el-radio :label="2">审核拒绝</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { export2Excel } from "@/components/commom.js";
export default {
  data() {
    return {
      ruleForm: {
        resource: ""
      },
      rules: {
        resource: [
          { required: true, message: "请选择审核是否通过", trigger: "change" }
        ]
      },
      selectCode: "", //模糊查询
      startTime: "", //开始时间
      endTime: "", //结束时间
      downloadLoading: false,
      dialogVisible: false, //弹窗审核
      tableData: [], //总条数
      total: 0, //总条数
      total: 0, //总条数
      current: 1, //页数
      userobj: {} //用户数据
    };
  },
  mounted() {
    this.merchantlist();
  },
  methods: {
    // 获取商家列表
    merchantlist() {
      let param = {
        status: 3,
        size: 10,
        current: this.current,
        startDate: this.startTime,
        endDate: this.endTime,
        selectCode: this.selectCode
      };
      this.$http.article.merchant(param).then(res => {
        this.tableData = res.result.records;
        this.total = res.result.total;
        console.log("获取商家列表", res);
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 分页数据
    handleCurrentChange(val) {
      this.current = val;
      this.merchantlist();
    },
    // 审核获取数据
    dialog(val) {
      this.dialogVisible = true;
      this.userobj = val;
      console.log("获取审核数据", val);
    },
    // 删除数据
    deletmerchant(val) {
      console.log("删除数据", val);
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var param = { petBusinessId: val.businessId };
          console.log("删除商家", param);
          this.$http.article.deletmerchant(param).then(res => {
            if (res.code == 200) {
              /*--------删除成功之后获取上一页的数据,避免是否为空--------*/

              //此时已经实现删除操作，所以total的值需要减size，Math.ceil是向上取整，保证始终大于等于0
              const totalPage = Math.ceil((this.total - 1) / 10);

              console.log("totalPage", totalPage);

              //将当前页码与删除数据之后的总页数进行比较，避免当前页码不存在
              const pagenum =
                this.current > totalPage ? totalPage : this.current;

              console.log("获取总条数", pagenum);

              //避免pagenum变为0
              this.current = pagenum < 1 ? 1 : pagenum;

              this.$message.success("删除成功!");

              this.merchantlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 确定-审核
    submitForm(formName) {
      console.log(this.userobj, "获取用户数据");
      this.$refs[formName].validate(valid => {
        if (valid) {
          let params = {
            businessId: this.userobj.businessId,
            businessStatus: this.ruleForm.resource
          };
          this.$http.article.updatamerchant(params).then(res => {
            if (res.code == 200) this.merchantlist();
            this.dialogVisible = false;
            // 关闭弹窗重置选择按钮
            this.resetForm("ruleForm");
            console.log("是否审核通过", res);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 查询
    search() {
       this.current=1;//模糊查询
      this.merchantlist();
      console.log(123);
    },
    // 重置查询
    reset() {
      this.merchantlist();
      this.selectCode = ""; //模糊查询
      this.startTime = ""; //开始时间
      this.endTime = ""; //结束时间
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 查看商家
    lookcheck(val) {
      console.log(val);
      this.$router.push({
        path: "/home/lookcheck",
        query: {
          businessId: val.businessId
        }
      });
    },
        // 关闭事件的回调
    closeDialog() {
      // this.ruleForm.resource = "";
      this.resetForm("ruleForm");
    },
    // 关闭弹窗
    // handleClose(done) {
    //   this.$confirm("确认关闭？")
    //     .then(_ => {
    //       // 关闭弹窗重置选择按钮
    //       this.resetForm("ruleForm");

    //       done();
    //     })
    //     .catch(_ => {});
    // },
    //   导出
    addcommodity() {
      console.log("导出");
      this.merchantlist();
      const hader = [
        {
          title: "联系人",
          date: "businessContact"
        },
        {
          title: "联系电话",
          date: "businessContactPhone"
        },
        {
          title: "商家名称",
          date: "businessName"
        },
        {
          title: "详细地址",
          date: "businessAddress"
        },
        {
          title: "营业时间",
          date: "businessHours"
        },
        {
          title: "服务介绍",
          date: "businessIntroduced"
        },
        {
          title: "店铺照片",
          date: "businessBanner"
        },
        {
          title: "店铺封面",
          date: "businessCoverImg"
        },
        {
          title: "营业执照",
          date: "businessLicense"
        },
        {
          title: "注册时间",
          date: "businessCreateTime"
        },
        {
          title: "状态",
          date: "businessStatus"
        }
      ];

      const excelName = "商家列表";

      let param = {
        status: 3,
        size: this.total,
        current: 1
      };
      this.$http.article.merchant(param).then(res => {
        const list = res.result.records.map(item => {
          //因为有枚举类型，所以这里对数据进行处理
          if (item.businessStatus) {
            item.businessStatus =
              item.businessStatus == 3 ? "待审核" : "待审核";
          }
          return item;
        });
        console.log('list',list)
        export2Excel(hader, list, excelName);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  background-color: #f4f7fd;
  padding-top: 10px;
}
.box-card {
  width: 98%;
  height: 80px;
  margin: 0 auto;
}
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>